package les.projeto;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class WeekView extends Activity{
	public Calendar week;
	public ArrayList<String> items; // container to store some random calendar items
	public WeekAdapter adapter;
	public Handler handler;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.week2);
	    week = Calendar.getInstance();
	    onNewIntent(getIntent());
	    items = new ArrayList<String>();
	    adapter = new WeekAdapter(this, week);
	    final GridView gridview = (GridView) findViewById(R.id.gridview3);
	    gridview.setAdapter(adapter);

	    
	    handler = new Handler();
	    handler.post(calendarUpdater);

        Button buttonMonthly = (Button)findViewById(R.id.buttonMonthly);
        buttonMonthly.setOnClickListener(new View.OnClickListener() {
        		 
        	@Override
        	public void onClick(View v) {
        		Calendar calendar = Calendar.getInstance();
		        int day = calendar.get(Calendar.DAY_OF_MONTH);
		        int month = calendar.get(Calendar.MONTH);
		        int year = calendar.get(Calendar.YEAR);
		        
		        Intent intent = new Intent(WeekView.this,CalendarView.class);
				intent.putExtra("date", year+"-"+month+"-"+day);
				WeekView.this.startActivity(intent);
				WeekView.this.finish();
        	} 
        });
	    
	    TextView title  = (TextView) findViewById(R.id.title);
	    title.setText(android.text.format.DateFormat.format("MMMM yyyy", week));
	    
	    TextView previous  = (TextView) findViewById(R.id.previous);
	    previous.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (week.get(Calendar.DAY_OF_MONTH) == week.getActualMinimum(Calendar.MONTH) ){
					if(week.get(Calendar.MONTH) == week.getActualMinimum(Calendar.MONTH)){
						week.set((week.get(Calendar.YEAR)-1),12,31);
					}else {
						week.set((week.get(Calendar.YEAR)),week.getActualMinimum(Calendar.MONTH)-1,1);
					}
				} else {
					week.set(Calendar.DAY_OF_MONTH,week.get(Calendar.DAY_OF_MONTH));
				}
				int day = week.get(Calendar.DAY_OF_MONTH) - week.get(Calendar.DAY_OF_WEEK) -7;
				week.set(Calendar.DAY_OF_MONTH,week.get(Calendar.DAY_OF_MONTH)-7);

	    		System.out.println(day);
	            int firstDay = (int)week.get(Calendar.DAY_OF_WEEK);
				if (day < firstDay ){
					day = week.getActualMaximum(Calendar.DAY_OF_MONTH) + day;
				}
	    		refreshCalendar(day);;
			}
		});
	    
	    TextView next  = (TextView) findViewById(R.id.next);
	    next.setOnClickListener(new OnClickListener() {
	
	    	@Override
			public void onClick(View v) {
	    		if(week.get(Calendar.MONTH) == week.getActualMaximum(Calendar.MONTH)){
	    			week.set((week.get(Calendar.YEAR)+1),1,1);
					System.out.println(week.get(Calendar.MONTH));
				}else if (week.get(Calendar.DAY_OF_MONTH) == week.getActualMaximum(Calendar.DAY_OF_MONTH)){
					week.set((week.get(Calendar.YEAR)),(week.get(Calendar.MONTH)+1),1);
				} else {
					week.set(Calendar.DAY_OF_MONTH,week.get(Calendar.DAY_OF_MONTH));
				}
	    		int day = week.get(Calendar.DAY_OF_MONTH) - week.get(Calendar.DAY_OF_WEEK) +7;
				week.set(Calendar.DAY_OF_MONTH,week.get(Calendar.DAY_OF_MONTH)+7);

	    		System.out.println(day);
				int lastDay = week.getActualMaximum(Calendar.DAY_OF_MONTH);
				if (day > lastDay ){
					day = lastDay - day;
				}
	    		refreshCalendar(day);
				
			}
				
		});
	    
		gridview.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		        
		        String day = adapter.days[position];
		        int monthInt = week.get(Calendar.MONTH);
		        int yearInt = week.get(Calendar.YEAR);
		        
		    	Intent trocatela = new Intent(WeekView.this, DayView.class);
		    	trocatela.putExtra("date", yearInt+"-"+monthInt+"-"+day);
		    	
		    	WeekView.this.startActivity(trocatela);
		    	WeekView.this.finish();	        		 
			    return;
		    }
		});
	}
	
	public void refreshCalendar(int number)
	{
		TextView title  = (TextView) findViewById(R.id.title);
	      
		adapter.refreshDays(number);
		adapter.notifyDataSetChanged();				
//		handler.post(calendarUpdater); // generate some random calendar items				
		
		title.setText(android.text.format.DateFormat.format("MMMM yyyy", week));
	}
	
	public void onNewIntent(Intent intent) {
		String date = intent.getStringExtra("date");
		String[] dateArr = date.split("-"); // date format is yyyy-mm-dd
		week.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
	}
	
	public Runnable calendarUpdater = new Runnable() {
		
		@Override
		public void run() {
			items.clear();
			// format random values. You can implement a dedicated class to provide real values
			for(int i=0;i<31;i++) {
				Random r = new Random();
				
				if(r.nextInt(10)>6)
				{
					items.add(Integer.toString(i));
				}
			}

			adapter.setItems(items);
			adapter.notifyDataSetChanged();
		}
	};
	
	@Override
	public void onBackPressed() {
	       Intent trocatela = new Intent(WeekView.this, ProjetoLESActivity.class);
	       WeekView.this.startActivity(trocatela);
	       WeekView.this.finish();	        		 
	       return;
	}

    private void addActivity() {
        int monthInt = week.get(Calendar.MONTH);
        int yearInt = week.get(Calendar.YEAR);
        
        Intent trocatela = new Intent(WeekView.this, AddActivityView.class);
        trocatela.putExtra("date", yearInt+"-"+monthInt+"-1");
        
        WeekView.this.startActivity(trocatela);
        WeekView.this.finish();
    }

    private void seeTeachers() {
        Intent trocatela = new Intent(WeekView.this, TeachersView.class);
        
        WeekView.this.startActivity(trocatela);
        WeekView.this.finish();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     super.onCreateOptionsMenu(menu);
     menu.add(0,0,0,"Add tasks");
     menu.add(1,1,1,"View teachers");
     return true;
    } 
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         super.onOptionsItemSelected(item);
         switch (item.getItemId()) {
            case 0:
                addActivity();
                break;
            case 1:
                seeTeachers();
                break;
         }
         return false;
    } 
}